/**
 * 
 */
package com.xxxx.game;

import java.util.Random;

import android.graphics.PointF;

/**
 * @author HuXiaolong
 *
 */
public class ShakeAnimation extends AnimationBasic implements IObjectPath
{
	private static final int max_live_time = 8;
	
	private int mLiveTime = max_live_time;
	
	private PointF mCenterPoint;
	private int mShakeDx = 1;
	private int mShakeDy = 1;
	
	public ShakeAnimation(PointF ptCenter, int dx, int dy)
	{
		mCenterPoint = ptCenter;
		mShakeDx = dx;
		mShakeDy = dy;
	}
	
	/* (non-Javadoc)
	 * @see com.xxxx.game.IAnimation#update(com.xxxx.game.ISprite)
	 */
	@Override
	public boolean update(ISprite sprite)
	{
		if (mLiveTime > 0)
		{
			if (-- this.mLiveTime > 0)
				sprite.setPosition(this.update(sprite.getPosition()));
			else
				sprite.setPosition(this.mCenterPoint);
		}
		return this.mLiveTime > 0;
	}

	/* (non-Javadoc)
	 * @see com.xxxx.game.IAnimation#getName()
	 */
	@Override
	public String getName()
	{
		// TODO Auto-generated method stub
		return "ShakeAnimation";
	}

	/* (non-Javadoc)
	 * @see com.xxxx.game.IAnimation#isMoving()
	 */
	@Override
	public boolean isMoving()
	{
		// TODO Auto-generated method stub
		return this.mLiveTime > 0;
	}

	/* (non-Javadoc)
	 * @see com.xxxx.game.IObjectPath#getCurPoint()
	 */
	@Override
	public PointF getCurPoint()
	{
		return this.mCenterPoint;
	}

	/* (non-Javadoc)
	 * @see com.xxxx.game.IObjectPath#update(android.graphics.PointF)
	 */
	@Override
	public PointF update(PointF curPt)
	{
		Random rand = new Random();
		int dx = rand.nextInt(this.mShakeDx * 2 + 1) - this.mShakeDx;
		int dy = rand.nextInt(this.mShakeDy * 2 + 1) - this.mShakeDy;
		
		PointF pt = new PointF(this.mCenterPoint.x + dx, this.mCenterPoint.y + dy);
		return pt;
	}

}
